#!/bin/bash

# 零售中台系统 Docker测试环境 API 验证测试脚本
# 用于验证Docker测试环境中的所有产品管理API是否正常工作

echo "=========================================="
echo "零售中台系统 Docker测试环境 API 验证测试"
echo "=========================================="

# 设置基础URL
BASE_URL="http://localhost:9080/retail-admin"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试函数
test_api() {
    local method=$1
    local url=$2
    local data=$3
    local description=$4
    
    echo -e "\n${YELLOW}测试: $description${NC}"
    echo "请求: $method $url"
    
    if [ "$method" = "GET" ]; then
        response=$(curl -s -w "\n%{http_code}" "$url")
    elif [ "$method" = "POST" ] || [ "$method" = "PUT" ]; then
        response=$(curl -s -w "\n%{http_code}" -X "$method" -H "Content-Type: application/json" -d "$data" "$url")
    elif [ "$method" = "DELETE" ]; then
        response=$(curl -s -w "\n%{http_code}" -X "$method" "$url")
    fi
    
    # 分离响应体和状态码
    http_code=$(echo "$response" | tail -n1)
    response_body=$(echo "$response" | sed '$d')
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✓ 成功 (HTTP $http_code)${NC}"
        echo "响应: $response_body"
    else
        echo -e "${RED}✗ 失败 (HTTP $http_code)${NC}"
        echo "响应: $response_body"
    fi
}

# 检查服务状态
echo -e "${YELLOW}检查服务状态...${NC}"

# 检查后端应用是否运行
if curl -s http://localhost:9080/retail-admin/api/version > /dev/null; then
    echo -e "${GREEN}✓ 后端应用运行中 (端口9080)${NC}"
else
    echo -e "${RED}✗ 后端应用未运行，请先启动应用${NC}"
    echo "运行命令: cd backend/retail-admin && mvn spring-boot:run -Dspring-boot.run.arguments=\"--server.port=9080\""
    exit 1
fi

# 检查MySQL容器是否运行
if docker ps | grep -q "retail-mysql-test"; then
    echo -e "${GREEN}✓ MySQL容器运行中${NC}"
else
    echo -e "${YELLOW}⚠ MySQL容器未运行，但后端应用可能使用其他数据库配置${NC}"
fi

# 等待服务完全启动
echo -e "${YELLOW}等待服务完全启动...${NC}"
sleep 10

# 1. 测试获取版本信息
test_api "GET" "$BASE_URL/api/version" "" "获取版本信息"

# 2. 测试健康检查API
test_api "GET" "$BASE_URL/api/version/health" "" "健康检查API"

# 3. 测试获取产品列表
test_api "GET" "$BASE_URL/api/products/page?pageNum=1&pageSize=10" "" "获取产品列表"

# 4. 测试获取产品详情
test_api "GET" "$BASE_URL/api/products/1" "" "获取产品详情"

# 5. 测试创建产品
product_data='{"name":"Docker测试商品","description":"这是通过Docker测试脚本创建的商品","categoryId":1,"brand":"Docker测试品牌"}'
test_api "POST" "$BASE_URL/api/products" "$product_data" "创建产品"

# 6. 测试更新产品
update_data='{"name":"更新后的Docker测试商品","description":"这是更新后的商品描述","categoryId":1,"brand":"更新Docker品牌"}'
test_api "PUT" "$BASE_URL/api/products/1" "$update_data" "更新产品"

# 7. 测试删除产品
test_api "DELETE" "$BASE_URL/api/products/1" "" "删除产品"

# 8. 测试API文档访问
test_api "GET" "$BASE_URL/doc.html" "" "API文档访问"

echo -e "\n=========================================="
echo "Docker测试环境 API 验证测试完成"
echo "=========================================="
echo "服务地址："
echo "  后端API: http://localhost:9080/retail-admin/"
echo "  API文档: http://localhost:9080/retail-admin/doc.html"
echo "  MySQL: localhost:3307"
echo "=========================================="
